package de.tabuma.customer.controller;

import de.tabuma.lib.*;
import de.tabuma.customer.*;
import de.tabuma.customer.entity.*;
import de.tabuma.customer.logik.*;
import java.io.*;
import java.util.*;
import java.util.logging.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.google.appengine.api.users.*;

/**
 *
 * @author alefen
 */
public class updateController extends tabumacontroller {

    private static final Logger log = Logger.getLogger(updateController.class.getName());

    public updateController(String servlet, Hashtable parameter) {
        super(servlet, parameter);
    }

    public void navigate(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out) {

        if(request.getParameter(constants.paramUpdate)!=null) {
            UserService userService = UserServiceFactory.getUserService();
            User user = userService.getCurrentUser();
            String id = request.getParameter(constants.paramId);
            int idInt = Integer.valueOf(id).intValue();
            String customerName = request.getParameter(constants.customerFieldName);
            Customer customer = CustomerFacade.get(idInt);
            customer.setName(customerName);
            CustomerFacade.update(customer, user.getEmail());
            listController listController = new listController(this.getServlet(), this.getParameter());
            listController.navigate(context, request, response, out);
        }
        else {
            request.setAttribute(constants.paramName, "" + constants.ACTION_UPDATE);
            String id = request.getParameter(constants.paramId);
            if(id!=null) {
                int idInt = Integer.valueOf(id).intValue();
                Customer customer = CustomerFacade.get(idInt);
                request.setAttribute(constants.paramCustomer, customer);
            }
        }
        
    }
    
}